<?php
class Detail extends Front_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('knowledge/knowledge_model','knowledge');
		$this->load->model('knowledge/knowledgefiles_model','knowledgefiles');
		$this->load->model('knowledgetag/knowledgetag_model','knowledgetag');
		$this->load->model('suggess_detail_model','suggess_detail');
		$this->load->model('suggess_result_model','suggess_result');
	}
	
	public function index($id=FALSE)
	{
			
			$this->knowledge->counter($id);
			$condition = "1=1";
			
			$conditions = "1=1";
			$data['dataList'] = $this->knowledge->get_row($id);

			$data['dataTags'] = $this->db->getarray("SELECT * FROM tbknowledgetag WHERE pid =".$id);
			$this->template->build('index',$data);	
			 
	}
	
	
	function counterfiles($idfile=FALSE)
	{
		if($_POST){
			if(@$_SESSION['file_id']>0){
				
				// download counter
				$idfile = $_SESSION['file_id'];
				$this->db->Execute("UPDATE tbknowledgefiles SET downloads = downloads + 1  WHERE id = ".$idfile);
				
				// suggess save
				$_POST['user_id'] = login_data('id');
				$_POST['tbknowledgefile_id'] = $idfile;
				$_POST['ip'] = $_SERVER["REMOTE_ADDR"];
				$_POST['created'] = date("Y-m-d H:i:s");
				$this->suggess_result->save($_POST);
				
				// download file
			    $filenames = $this->db->getone("SELECT filename FROM tbknowledgefiles WHERE id = ".$idfile);
				$this->load->helper('download');
				$data = file_get_contents("knowledgefiles/".basename( $filenames));
				$name = basename( $filenames);
				force_download($name, $data);
			}
		}
	}

	function suggestion($file_id){
		$data['suggess_details'] = $this->suggess_detail->order_by('id','asc')->get();
		$_SESSION['file_id'] = $file_id;
		$this->template->build('suggestion',$data);
	}
}
?>